//
//  PaiBanListView.m
//  WatermarkCamera
//
//  Created by zhibao on 2023/9/21.
//

#import "PaiBanListView.h"
@interface PaiBanListView(){
    LiPaiType type;
    NSArray *contents;
    NSMutableArray *btns;
}
@end
@implementation PaiBanListView
- (instancetype)initWithType:(LiPaiType)selectType{
    self = [super initWithFrame:CGRectMake(0, MY_Screen_H, MY_Screen_W, MY_Screen_H)];
    if(self){
        type = selectType;
        btns = [NSMutableArray new];
        contents = @[
            @{@"select":@"h_pb_henger_selected",@"img":@"h_pb_henger",@"title":LocalizedString(@"单排")},
            @{@"select":@"h_pb_shuer_selected",@"img":@"h_pb_shuer",@"title":LocalizedString(@"双排")},
            @{@"select":@"h_pb_si_selected",@"img":@"h_pb_si",@"title":LocalizedString(@"四宫格")},
            @{@"select":@"h_pb_liu_selected",@"img":@"h_pb_liu",@"title":LocalizedString(@"六宫格")},
            @{@"select":@"h_pb_jiu_selected",@"img":@"h_pb_jiu",@"title":LocalizedString(@"九宫格")},
        ];
        [self initView];
    }
    return self;
}

-(void)initView{
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, MY_Screen_H - 410, MY_Screen_W, 410 + 20)];
    topView.layer.cornerRadius = 20;
    topView.backgroundColor = UIColor.whiteColor;
    [self addSubview:topView];
    
    if(topView){
        UILabel *topTitleLabel = [UILabel new];
        topTitleLabel.text = LocalizedString(@"排版");
        topTitleLabel.font = MY_SCALE_SemiboldFONT(16);
        topTitleLabel.textColor = MYBlock;
        [topView addSubview:topTitleLabel];
        [topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(topView);
            make.top.mas_equalTo(16);
        }];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setImage:[UIImage imageNamed:@"com_close_qian"] forState:UIControlStateNormal];
        [topView addSubview:cancelBtn];
        [cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        cancelBtn.tag = 1000;
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.right.mas_equalTo(-15);
        }];
    }
    CGFloat space = (MY_Screen_W - 4 * 75) / 5;
    for(int i = 0; i < contents.count; i++){
        int heng = i / 4;
        int shu = i % 4;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(space + (75 + space) * shu, topView.top + 63 + (100 + 20) * heng, 75, 100);
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:btn.bounds];
        imgView.tag = 10001;
        imgView.image = [UIImage imageNamed:contents[i][@"img"]];
        [btn addSubview:imgView];
        
        UILabel *label = [UILabel new];
        label.text = contents[i][@"title"];
        label.font = MY_SCALE_SemiboldFONT(12);
        label.textColor = MYBlock;
        label.tag = 10002;
        label.textAlignment = NSTextAlignmentCenter;
        [imgView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(imgView);
            make.top.mas_equalTo(10);
        }];
        if(i == type){
            imgView.image = [UIImage imageNamed:contents[i][@"select"]];
            label.textColor = MyColor(@"#12BFFF");
        }
        [btns addObject:btn];
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(15, 315, MY_Screen_W - 30, 46);
    btn.layer.cornerRadius = 23;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = MyColor(@"#12BFFF");
    [topView addSubview:btn];
    btn.tag = 1001;
    [btn setTitle:LocalizedString(@"完成") forState:UIControlStateNormal];
    btn.titleLabel.font = MY_SCALE_SemiboldFONT(16);
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
}

-(void)btnClick:(UIButton *)sender{
    if(sender.tag == 1000){
        
    }else if(sender.tag == 1001){
        int num = 0;
        if(type == LiPaiTypeShuangPai){
            num = 2;
        }else if(type == LiPaiTypeDanPai){
            num = 2;
        }else if(type == LiPaiTypeSi){
            num = 4;
        }else if(type == LiPaiTypeLiu){
            num = 6;
        }else if(type == LiPaiTypeJiu){
            num = 9;
        }
        if(self.paiBanListBlock){
            self.paiBanListBlock(type,num);
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.top = MY_Screen_H;
    }];
}

-(void)selectBtnClick:(UIButton *)sender{
    for(int i = 0; i < btns.count; i++){
        UIButton *btn = btns[i];
        UIImageView *imgView = [btn viewWithTag:10001];
        UILabel *label = [btn viewWithTag:10002];
        imgView.image = [UIImage imageNamed:contents[i][@"img"]];
        label.textColor = MYBlock;
    }
    
    UIImageView *imgView = [sender viewWithTag:10001];
    UILabel *label = [sender viewWithTag:10002];
    imgView.image = [UIImage imageNamed:contents[sender.tag - 1000][@"select"]];
    label.textColor = MyColor(@"#12BFFF");
    type = sender.tag - 1000;
}
- (void)retrunLast:(LiPaiType)type{
    UIButton *btn = btns[type];
    [self selectBtnClick:btn];
}

@end
